cd "/Users/mmoral/Documents/The Story of Electoral Alliances/"
clear all
* version 16								/* This line should be uncommented for Stata versions newer than 16 */

/* See the Excel file for the calculations of the party vote shares in the new districts */
import excel "All Elections.xlsx", sheet("June 2015") firstrow clear
save "June2015.dta", replace

import excel "All Elections.xlsx", sheet("November 2015") firstrow clear
save "Nov2015.dta", replace

** Without Alliances
* 2015 November Election (Actual and Scenario 1 - 600 MPs and with threshold)
use "Nov2015.dta", clear

gen total=1
replace total=0 if _n>=7 & _n<=11 | _n>=22 & _n<=23
gen old=1									/* Ankara 2 districts, Bursa a single district */
replace old=0 if _n==6 | _n>=9 & _n<=11 | _n>=22 & _n<=23
gen new=1									/* Ankara 3 districts, Bursa 2 districts */
replace new=0 if _n>=6 & _n<=8 | _n==21

rename (AKPARTİ CHP MHP HDP BBP SP DP Other) (party#), addnumber
reshape long party, i(district) j(par)  
egen dis=group(district)

v2seats party if old==1, party(par) formula(dhondt) district(dis) athres(10) size(Total_MP)
v2seats party if new==1, party(par) formula(dhondt) district(dis) athres(10) size(New_MP)

disp (344-317)+ (141-134)+ (48-40) +(67-59) /* Control */
disp (344/600)-(317/550) 					/* AKP */
disp (141/600)-(134/550) 					/* CHP */
disp (48/600)-(40/550) 						/* MHP */
disp (67/600)-(59/550) 						/* HDP */

* 2015 June Election (Actual and Scenario 1 - 600 MPs and with threshold)
use "June2015.dta", clear

gen total=1
replace total=0 if _n>=7 & _n<=11 | _n>=22 & _n<=23
gen old=1									/* Ankara 2 districts, Bursa a single district */
replace old=0 if _n==6 | _n>=9 & _n<=11 | _n>=22 & _n<=23
gen new=1									/* Ankara 3 districts, Bursa 2 districts */
replace new=0 if _n>=6 & _n<=8 | _n==21

rename (AKPARTİ CHP MHP HDP SP DP Other) (party#), addnumber
drop if missing(district)
reshape long party, i(district) j(par)  
egen dis=group(district)

v2seats party if old==1, party(par) formula(dhondt) district(dis) athres(10) size(Total_MP)
v2seats party if new==1, party(par) formula(dhondt) district(dis) athres(10) size(New_MP)

disp (258-284)+ (132-142)+ (80-89) +(80-85) /* Control */
disp (284/600)-(258/550) 					/* AKP */
disp (142/600)-(132/550) 					/* CHP */
disp (89/600)-(80/550) 						/* MHP */
disp (85/600)-(80/550) 						/* HDP */

** With Alliances
* November 2015 Election (Scenario 2 - 600 MPs and without threshold)
use "Nov2015.dta", clear
gen new=1									/* Ankara 3 districts, Bursa 2 districts */
replace new=0 if _n>=6 & _n<=8 | _n==21

gen coal1=AKPARTİ+MHP+BBP
gen coal2=CHP+SP+DP
rename (AKPARTİ MHP BBP CHP SP DP HDP Other coal1 coal2) (party#), addnumber
drop if missing(district)
reshape long party, i(district) j(par)  
egen dis=group(district)

v2seats party if new==1 & par>6, party(par) formula(dhondt) district(dis) athres(10) size(New_MP) save(sim2_nov)

disp 401+135+64

preserve
use "sim2_nov", clear
decode distl1, gen(dis) 
decode party, gen(par)
destring dis par, replace
drop distl1 party
save "sim2_nov", replace
restore 

merge 1:1 dis par using "sim2_nov", nogen

gen cumhur=.a
gen millet=.a
capture noisily forval i=1/91{
qui sum dhondt if par==9 & dis==`i' 
qui replace cumhur=(r(mean)) if dis==`i'
qui sum dhondt if par==10 & dis==`i' 
qui replace millet=(r(mean)) if dis==`i'
}

keep if inlist(par, 1, 2)
recode cumhur (0=.)
v2seats party if new==1, party(par) formula(dhondt) district(dis) size(cumhur)

disp (344+48+141+67)-(352+49+135+64) 		/* Control */
disp (352/600)-(344/600) 					/* AKP */
disp (49/600)-(48/600) 						/* MHP */
disp (135/600)-(141/600)					/* CHP */
disp (64/600)-(67/600)						/* HDP */

* June 2015 Election (Scenario 2 - 600 MPs and without threshold)
use "June2015.dta", clear
gen new=1									/* Ankara 3 districts, Bursa 2 districts */
replace new=0 if _n>=6 & _n<=8 | _n==21

gen coal1=AKPARTİ+MHP
gen coal2=CHP+SP+DP
rename (AKPARTİ MHP CHP SP DP HDP Other coal1 coal2) (party#), addnumber
drop if missing(district)
reshape long party, i(district) j(par)  
egen dis=group(district)

v2seats party if new==1 & par>5, party(par) formula(dhondt) district(dis) athres(10) size(New_MP) save(sim2_june)

disp 374+145+81

preserve
use "sim2_june", clear
decode distl1, gen(dis) 
decode party, gen(par)
destring dis par, replace
drop distl1 party
save "sim2_june", replace
restore 

merge 1:1 dis par using "sim2_june", nogen

gen cumhur=.a
gen millet=.a
capture noisily forval i=1/91{
qui sum dhondt if par==8 & dis==`i' 
qui replace cumhur=(r(mean)) if dis==`i'
qui sum dhondt if par==9 & dis==`i' 
qui replace millet=(r(mean)) if dis==`i'
}

keep if inlist(par, 1, 2)
recode cumhur (0=.)
v2seats party if new==1, party(par) formula(dhondt) district(dis) size(cumhur)

disp (284+89+142+85)-(286+88+145+81) 		/* Control */
disp (286/600)-(284/600) 					/* AKP */
disp (145/600)-(142/600) 					/* CHP */
disp (88/600)-(89/600) 						/* MHP */
disp (81/600)-(85/600) 						/* HDP */
